package com.sdp.right.control;


import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.threadLocal.ContextManager;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.db.sys_emp.service.ISysEmpService;
import com.sdp.right.db.sys_role_person.service.ISysRolePersonService;
import com.sdp.right.entity.SysEmp;
import com.sdp.right.utils.RightConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/SysEmpController")
public class SysEmpController {

	@Autowired
	private ISysEmpService iSysEmpService;
	
	@Autowired
	private ISysRolePersonService iSysRolePersonService;

	@Autowired
	HttpServletRequest request;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private Query query;
	
	

	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
//		DynamicDataSourceContextHolder.push("right2");
		IPage<SysEmp> p = query.page(iSysEmpService, SysEmp.class, dataCenter);
//		System.out.println(p.getTotal());
//		DynamicDataSourceContextHolder.clear();
		return Result.ok(p);
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
//	@OperLog(operSystemNo="right",operDesc="人员档案")
	public Result<?> save(@RequestBody SysEmp sysEmp) throws NoSuchAlgorithmException {
		if(sysEmp==null) {
			return Result.error("人员信息为空");
		}
		if(StringUtils.isEmpty(sysEmp.getKeyId())) {
			//账号不能重复和工号不能重复
			Result<?> r = uniqueCheckTools.isUniqueSingle(iSysEmpService, SysEmp.class, new String[] {"empNo","accountNo"}, "keyId", sysEmp, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("账号或者工号重复");
			}
			sysEmp.setIsUse(RightConstants.是.getCode());
			sysEmp.setEmpNo(sysEmp.getAccountNo());
			MessageDigest md = MessageDigest.getInstance("MD5");
	        md.update(RightConstants.默认密码.getCode().getBytes());
	        sysEmp.setEmpPwd(new BigInteger(1, md.digest()).toString(16));
	        iSysEmpService.save(sysEmp);
	        
	        //添加人员角色
	        iSysRolePersonService.addRolePerson(sysEmp, RightConstants.默认角色.getCode());

		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iSysEmpService, SysEmp.class, new String[] {"empNo","accountNo"}, "keyId", null, sysEmp, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("账号或者工号重复");
			}
			iSysEmpService.updateById(sysEmp);
		}
		
		return Result.ok();
	}
	
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId,@RequestParam(name="accountNo",required=true) String accountNo) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		//删除人员
		iSysEmpService.removeById(keyId);
		
		//删除人员角色
		iSysRolePersonService.removeSysRolePersonByAccountNo(accountNo);
		return Result.ok();
	}
	
	@GetMapping(value = "/freeze")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> freeze(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要冻结的记录信息不完整");
		}
		SysEmp sysEmp = iSysEmpService.getById(keyId);
		if(sysEmp==null) {
			return Result.error("需要冻结的记录不存在");
		}
		sysEmp.setIsUse(RightConstants.否.getCode());
		iSysEmpService.updateById(sysEmp);
		return Result.ok();
	}
	
	@GetMapping(value = "/unfreeze")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> unfreeze(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要解冻的记录的信息不完整");
		}
		SysEmp sysEmp = iSysEmpService.getById(keyId);
		if(sysEmp==null) {
			return Result.error("需要解冻的记录不存在");
		}
		sysEmp.setIsUse(RightConstants.是.getCode());
		iSysEmpService.updateById(sysEmp);
		return Result.ok();
	}
	

	@GetMapping(value = "/resetPwd")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> resetPwd(@RequestParam(name="keyId",required=true) String keyId) throws NoSuchAlgorithmException {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要密码重置的记录的信息不完整");
		}
		SysEmp sysEmp = iSysEmpService.getById(keyId);
		if(sysEmp==null) {
			return Result.error("需要密码重置的记录不存在");
		}
		MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(RightConstants.默认密码.getCode().getBytes());
        sysEmp.setEmpPwd(new BigInteger(1, md.digest()).toString(16));
        iSysEmpService.updateById(sysEmp);
		return Result.ok();
	}

	
	/**
	 * 修改密码
	 */
	@PostMapping(value = "/changePwd")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> changePwd(@RequestBody SysEmp sysEmp) {
		if(StringUtils.isEmpty(sysEmp)) {
			return Result.error("输入信息不完整或者登陆过期，请重新登陆再试");
		}
		
		String loginAccountNo = ContextManager.getContextData(CoreConstantsEnum.token中账号的属性名.getCode()).toString();
		if(StringUtils.isEmpty(loginAccountNo)) {
			return Result.error("请先登录");
		}
		if(!loginAccountNo.equals(sysEmp.getAccountNo())) {
			return Result.error("只能修改当前登录人的密码");
		}
		
		
		
		if(iSysEmpService.changePwd(sysEmp)) {
			return Result.ok();
		}else {
			return Result.error("修改密码失败");
		}
		
	}
	
	@PostMapping(value = "/changeDefaultPwd")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> changeDefaultPwd(@RequestBody SysEmp sysEmp) {
		if(StringUtils.isEmpty(sysEmp) || StringUtils.isEmpty(sysEmp.getAccountNo()) || StringUtils.isEmpty(sysEmp.getEmpPwd())) {
			return Result.error("输入信息不完整");
		}
		
		if(iSysEmpService.changePwd(sysEmp)) {
			return Result.ok();
		}else {
			return Result.error("修改密码失败");
		}
		
	}
	
	
	@GetMapping(value = "/querySysEmpByEmpNo")
	public Result<?> querySysEmpByEmpNo(@RequestParam(name="empNo",required=true) String empNo) {
		return Result.ok(iSysEmpService.querySysEmpByEmpNo(empNo));
	}
}

